package com.yc.thread.pro2_knn.knn;

import com.yc.thread.pro2_knn.bean.Sample;

import java.util.Map;

/**
 * 欧式距离计算方法
 * @author zwei
 * @create 2024-07-17 20:09
 */
public class EuclideanDistanceCalculator implements DistanceCalculator{
    //父类引用指向子类对象
    @Override
    public double calculate(Sample s1, Sample s2){
        double result=0.0;
        double[]data1=s1.getExample();      //样本1的数据
        double[]data2=s2.getExample();      //样本2的数据
        if (data1.length!=data2.length){
            throw new RuntimeException("样本维度不同");
        }
        for (int i=0;i<data1.length;i++) {
            result += Math.pow(data1[i] - data2[i], 2);
        }
        return Math.sqrt(result);
    };

}
